﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WI.CandidateManager.Common.Data.Entities;
using WI.CandidateManager.Common.Data.Enums;

namespace WI.CandidateManager.Business.Objects
{
    public class ScheduleBO : BOBase
    {
        #region CRUDSchedule

        /// <summary>
        /// Creates the schedule.
        /// </summary>
        /// <param name="schedule">The schedule.</param>
        /// <returns></returns>
        public bool CreateSchedule(Schedule schedule)
        {
            try
            {
                Logger.Info("Try to create schedule");
                schedule.State = State.Active;
                DataContext.ScheduleSet.Add(schedule);
                DataContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error("Can't create schedule", ex);
                return false;
            }
        }

        /// <summary>
        /// Deletes the schedule.
        /// </summary>
        /// <param name="subjectId">The subject id.</param>
        /// <param name="groupId">The group id.</param>
        /// <returns></returns>
        public bool DeleteSchedule(long subjectId, long groupId)
        {
            try
            {
                Logger.Info(String.Format("Try to delete schedule for subjectId: {0} and groupId: {1}", subjectId, groupId));
                var schedule = DataContext.ScheduleSet.Where(s =>
                    s.StudyGroupId == groupId &&
                    s.SubjectId == subjectId).Single();
                foreach (var occurrence in schedule.Occurrences)
                {
                    new OccurrenceBO().DeleteOccurence(occurrence.Id);
                }
                schedule.State = State.Deleted;
                DataContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(String.Format("Can't delete schedule for subjectId: {0} and groupId: {1}", subjectId, groupId), ex);
                return false;
            }
        }

        /// <summary>
        /// Gets the schedules.
        /// </summary>
        /// <param name="subjectId">The subject id.</param>
        /// <param name="groupId">The group id.</param>
        /// <returns></returns>
        public IEnumerable<Schedule> GetSchedules(long? subjectId = null, long? groupId = null)
        {
            return DataContext.ScheduleSet.Where(s =>
                (!subjectId.HasValue || s.SubjectId == subjectId) &&
                (!groupId.HasValue || s.StudyGroupId == groupId) &&
                (s.State == State.Active));
        }

        /// <summary>
        /// Updates the specified schedule.
        /// </summary>
        /// <param name="schedule">The schedule.</param>
        /// <returns></returns>
        public bool Update(Schedule schedule)
        {
            try
            {
                Logger.Info("Try to update schedule");
                var updateSchedule = DataContext.ScheduleSet.Where(s =>
                    s.SubjectId == schedule.SubjectId &&
                    s.StudyGroupId == schedule.StudyGroupId).Single();
                updateSchedule.StartDate = schedule.StartDate;
                updateSchedule.EndDate = schedule.EndDate;
                updateSchedule.State = schedule.State;
                DataContext.Entry(updateSchedule).State = EntityState.Modified;
                DataContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error("Can't update schedule", ex);
                return false;
            }
        }

        #endregion

    }
}
